package com.pig.provide.http.controller;

import com.pig.common.RpcConst;
import com.pig.provide.http.context.MethodSupport;
import com.pig.provide.http.context.RpcRequestContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;

/**
 * @author Administrator
 */
@RestController
@Slf4j
public class ProviderController extends BaseController{
    @Autowired
    HttpServletRequest request;
    @Autowired
    ApplicationContext applicationContext;
    @Autowired
    MethodSupport methodSupport;

    @RequestMapping("/provider")
    public String provider() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        String methodInfoStr = request.getParameter(RpcConst.REQUEST_INFO);
        String argsStr = request.getParameter(RpcConst.ARGS);
        //根据请求参数，解析出请求的上下文
        RpcRequestContext context = RpcRequestContext.newContext(methodInfoStr,argsStr);
        String result = methodSupport.invoke(context);
        return result;
    }

}
